/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.persistence.dao;

import com.edu.kamai.persistence.beans.Paper;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author xyzhu
 */
public class PaperDao {
    private static final Log log = LogFactory.getLog(Paper.class);
    public static final String PAPER_BEAN = "com.edu.kamai.persistence.beans.Paper";
    private Session session = null;
    
    public PaperDao(){
        this.session  = kamaiHibernateUtil.getSessionFactory().openSession();
    }
    
    public void addPaper(Paper paper){
        log.debug("save paper instance");
        try{
            Transaction tx = session.beginTransaction();
            session.save(paper);
            tx.commit();
            log.debug("save successful");
            
        }catch(RuntimeException re){
            log.error("save failed", re);
            throw re;
        }
    }
    
    public void updatePaper(Paper paper){
        log.debug("update paper instance");
        try{
            Transaction tx = session.beginTransaction();
            session.update(paper);
            tx.commit();
            log.debug("update successful");
            
        }catch(RuntimeException re){
            log.error("update failed",re);
            throw re;
        }
    }
    
    public List<Paper> findPapers(int roleID){
        log.debug("check the existing papers!");
        try{
            Transaction tx = session.beginTransaction();
            String hqlStr=new String("from Paper where status=" + roleID);
            List<Paper> papers = session.createQuery(hqlStr).list();
            tx.commit();
            return papers;
        }catch(Exception e){
            log.error("check the pending list error",e);
            return null;
        }
    }
    
    public List<Paper> findPapers(){
        log.debug("check the existing papers!");
        try{
            Transaction tx = session.beginTransaction();
            String hqlStr=new String("from Paper");
            List<Paper> papers = session.createQuery(hqlStr).list();
            tx.commit();
            return papers;
        }catch(Exception e){
            log.error("check the pending list error",e);
            return null;
        }
    }
    
    public Paper findPaperByIDAndRole(int roleID,int pid){
        log.debug("check the paper by id and role!");
        try{
            Transaction tx = session.beginTransaction();
            String hqlStr=new String("from Paper where status=" + roleID + " and paper_id=" + pid);
            List<Paper> list = session.createQuery(hqlStr).list();
            tx.commit();
            return list.size() == 0 ? null : list.get(0);
        }catch(Exception ex){
            log.error("check the paper by id and role error",ex);
            return null;
        }
        
    }
    
    
}
